package com.example.demo.controller;

import com.example.demo.pojo.Course;
import com.example.demo.pojo.Student;
import com.example.demo.service.StudentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/student")
@Transactional
public class StudentContrller {

    @Autowired
    private StudentService studentService;
    @ApiOperation("跳转到用户信息表")
    @RequestMapping("/toUpdatePage")
    public String toUpdatePage(){
        return "student/updateInfo";
    }
    @ApiOperation("student信息修改")
    @RequestMapping("/updateInfo")
    public String updateInfo(@RequestParam("sno") int sno,
                             @RequestParam("sname") String sname,
                             @RequestParam("ssex") String ssex,
                             @RequestParam("sdept") String sdept,
                             @RequestParam("sclass") String sclass,
                             @RequestParam("uid") String uid ,
                             @RequestPart("face") MultipartFile face){
        if(!face.isEmpty()){
            try {
                //获取文件后缀名
                String originalFilename=face.getOriginalFilename();
                String finalFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                face.transferTo(new File("D://photo//"+finalFileName));
                String path="/Path1/"+finalFileName;
                Student student=new Student(sno,sname,ssex,sdept,sclass,uid,path);
                studentService.updateInfo(student);
            }catch (Exception e){
               e.printStackTrace();
            }
        }
        return "redirect:/student/toUpdatePage";
    }
    @ApiOperation("获取student已选课程")
    @RequestMapping("/getAllCourse")
    public String getAllCourse(HttpSession session, Model model){
        Student student= (Student) session.getAttribute("student");
        List<Course> list=studentService.getAllCourse(student.getSno());
        model.addAttribute("courses",list);
        return "/student/hasCourse";
    }
}
